package com.dvj.gulimall.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.fastjson.JSON;
import com.dvj.common.exception.BizCodeEnum;
import com.dvj.common.utils.R;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

@Configuration
public class SentinelGatewayConfig {

    //TODO 响应式编程
    //GatewayCallbackManager
    public SentinelGatewayConfig() {
        //网关限流了请求，就会调用此回调  Mono Flux
        GatewayCallbackManager.setBlockHandler((exchange, t) -> {

            R error = R.error(BizCodeEnum.TOO_MANY_REQUEST);
            String errJson = JSON.toJSONString(error);

//                Mono<String> aaa = Mono.just("aaa");
            Mono<ServerResponse> body = ServerResponse.ok().body(Mono.just(errJson), String.class);
            return body;
        });

//        FlowRule flowRule = new FlowRule();
//        flowRule.setRefResource("gulimall_seckill_route");
////        flowRule.set
//        FlowRuleManager.loadRules(Arrays.asList(flowRule));
    }
}
